package com.waypolice.manager.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.waypolice.common.pojo.DestroyCommonResult;
import com.waypolice.common.pojo.TaotaoResult;
import com.waypolice.common.utils.PageUtils;
import com.waypolice.manager.service.*;
import com.waypolice.pojo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


/**
 * 档案著录
 * @author zry
 *
 */
@Controller
@Api(value = "档案", description = "档案鉴定")
@RequestMapping(value="/pcwindow")
public class ArcDestroyAction {

	private static final Logger LOGGER = LoggerFactory.getLogger(ArcDestroyAction.class);

    @Autowired
    private ArcAppraisalService arcAppraisalService;
    @Autowired
    private ArcDestroyService arcDestroyService;
    @Autowired
    private VolAndItemService volAndItemService;
    @Autowired
    private UpmsArchivesTypesService upmsArchivesTypesService;
    @Autowired
    private ArcDestroyArchiveService arcDestroyArchiveService;
    @Autowired
    private UpmsDictionaryService upmsDictionaryService;
	/**
	 * 档案销毁页面跳转
	 *
	 * @param map
	 * @param response
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/arcDestroy")
	public String destroyManagerJump(ModelMap map, HttpServletResponse response,
								  HttpServletRequest request) {
        String sign = request.getParameter("sign");
        if(StringUtils.isNotBlank(sign)) {
            request.setAttribute("sign", sign);
        }
		return "/destroyArchive/destroyArchive";
	}
    @ApiOperation(value = "查询所有的待销毁的卷信息")
    @RequestMapping(value = "/searchNeedDestroyVolumeMessage", method = RequestMethod.GET)
    @ResponseBody
    public Object  searchNeedDestroyVolumeMessage(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
            Map<String, Object> result = new HashMap<>(2);
            List<DestroyCommonResult> des = new ArrayList<DestroyCommonResult>();
            //查询件
            List<CmsAppraisal> cmsAppraisals =  arcDestroyService.searchNeedDestroyVolumeMessage();
            for (CmsAppraisal cmsAppraisal:cmsAppraisals) {
                String apprCode = cmsAppraisal.getApprCode();
                Integer apprType = cmsAppraisal.getApprType();
                String appExtrafive = cmsAppraisal.getAppExtrafive();
                String appExtrasix = cmsAppraisal.getAppExtrasix();
                Long volId = cmsAppraisal.getApprArcId();//卷Id
                if(apprType == 0){
                    DestroyCommonResult  destroyCommonResult = new DestroyCommonResult();
                    CmsJudVolume cmsJudVolume = volAndItemService.selectCmsJudVolumeByVolId(Long.valueOf(volId));
                    if(!StringUtils.isBlank(cmsJudVolume.getVolClass())){
                        destroyCommonResult.setVolClass(cmsJudVolume.getVolClass());
                    }
                    if(!StringUtils.isBlank(cmsJudVolume.getVolSecret())){
                        destroyCommonResult.setVolSecret(cmsJudVolume.getVolSecret());
                    }
                    if(!StringUtils.isBlank(cmsJudVolume.getVolTotalPage())){
                        destroyCommonResult.setVolTotalPage(cmsJudVolume.getVolTotalPage());
                    }
                    if(!StringUtils.isBlank(cmsJudVolume.getVolStorageTime())){
                        destroyCommonResult.setVolStorageTime(cmsJudVolume.getVolStorageTime());
                    }
                    if(!StringUtils.isBlank(cmsJudVolume.getVolStorageTime())){
                        destroyCommonResult.setVolStorageTime(cmsJudVolume.getVolStorageTime());
                    }
                    if(!StringUtils.isBlank(cmsJudVolume.getVolTitle())){
                        destroyCommonResult.setVolTitle(cmsJudVolume.getVolTitle());
                    }
                    if(!"".equals(cmsJudVolume.getVolArchiveDate())){
                        destroyCommonResult.setVolArchiveDate(cmsJudVolume.getVolArchiveDate());
                    }
                    if(!StringUtils.isBlank(cmsJudVolume.getVolCaseNum())){
                        destroyCommonResult.setVolCaseNum(cmsJudVolume.getVolCaseNum());
                    }
                    destroyCommonResult.setItemName("");
                    destroyCommonResult.setItemRespons("");
                    Long itemNumById = arcAppraisalService.findItemNumById(volId.toString());
                    destroyCommonResult.setItemTotalNumber(itemNumById.toString());
                    destroyCommonResult.setArcSign(0);
                    destroyCommonResult.setArcId(volId);
                    destroyCommonResult.setApprCode(apprCode);
                    destroyCommonResult.setApprOldDate(appExtrafive);
                    destroyCommonResult.setApprRemarks(appExtrasix);
                    des.add(destroyCommonResult);
                }else if(apprType == 1){
                    DestroyCommonResult  destroyCommonResult = new DestroyCommonResult();
                    List<Map<String,Object>>  arichives = arcAppraisalService.findArchiveTypeByArchiveNum(volId.toString());
                    for (Map<String,Object> map:arichives) {
                        if(map.containsKey("VOL_CLASS")){
                            destroyCommonResult.setVolClass(map.get("VOL_CLASS").toString());
                        }
                        if(map.containsKey("ITEM_SECRET")){
                            destroyCommonResult.setVolSecret(map.get("ITEM_SECRET").toString());
                        }
                        //保存期限
                        if(map.containsKey("VOL_STORAGE_TIME")){
                            destroyCommonResult.setVolStorageTime(map.get("VOL_STORAGE_TIME").toString());
                        }
                        if(map.containsKey("ITEM_TITLE")){
                            destroyCommonResult.setVolTitle(map.get("ITEM_TITLE").toString());
                        }
                        if(map.containsKey("ITEM_PAGE")){
                            destroyCommonResult.setVolTotalPage(map.get("ITEM_PAGE").toString());
                        }
                        if(map.containsKey("ITEM_STR11_DATE")){
                            destroyCommonResult.setVolArchiveDate((Date)map.get("ITEM_STR11_DATE"));
                        }
                        if(map.containsKey("ITEM_STR1")){
                            destroyCommonResult.setVolCaseNum(map.get("ITEM_STR1").toString());
                        }
                        if(map.containsKey("ITEM_NAME")){
                            destroyCommonResult.setItemName(map.get("ITEM_NAME").toString());
                        }
                        if(map.containsKey("ITEM_RESPONS")){
                            destroyCommonResult.setItemRespons(map.get("ITEM_RESPONS").toString());
                        }
                        destroyCommonResult.setItemTotalNumber("1");
                        destroyCommonResult.setArcSign(1);
                        destroyCommonResult.setArcId(volId);
                        destroyCommonResult.setApprCode(apprCode);
                        destroyCommonResult.setApprOldDate(appExtrafive);
                        destroyCommonResult.setApprRemarks(appExtrasix);
                        des.add(destroyCommonResult);
                    }
                }else{
                    DestroyCommonResult  destroyCommonResult = new DestroyCommonResult();
                    List<CmsSinItem> cmsSinItems = arcAppraisalService.selectSinItemByItemId(Long.valueOf(volId));
                    CmsSinItem cmsSinItem = cmsSinItems.get(0);
                    //案卷分类
                    if(!StringUtils.isBlank(cmsSinItem.getItemExp1())){
                        destroyCommonResult.setVolClass(cmsSinItem.getItemExp1());
                    }
                    //密级
                    if(!StringUtils.isBlank(cmsSinItem.getItemSecret())){
                        destroyCommonResult.setVolSecret(cmsSinItem.getItemSecret());
                    }
                    //页数
                    if(!StringUtils.isBlank(cmsSinItem.getItemPage().toString())){
                        destroyCommonResult.setVolTotalPage(cmsSinItem.getItemPage().toString());
                    }
                    if(!StringUtils.isBlank(cmsSinItem.getItemNum())){
                        destroyCommonResult.setVolStorageTime(cmsSinItem.getItemNum());
                    }
                    //题名
                    if(!StringUtils.isBlank(cmsSinItem.getItemTitle())){
                        destroyCommonResult.setVolTitle(cmsSinItem.getItemTitle());
                    }
                    //归档日期
                    if(!"".equals(cmsSinItem.getItemStr1Date())){
                        destroyCommonResult.setVolArchiveDate(cmsSinItem.getItemStr1Date());
                    }
                    //档号
                    if(!StringUtils.isBlank(cmsSinItem.getItemStr1())){
                        destroyCommonResult.setVolCaseNum(cmsSinItem.getItemStr1());
                    }
                    //文号
                    if(!StringUtils.isBlank(cmsSinItem.getItemName())){
                        destroyCommonResult.setItemName(cmsSinItem.getItemName());
                    }
                    //责任者
                    if(!StringUtils.isBlank(cmsSinItem.getItemRespons())){
                        destroyCommonResult.setItemRespons(cmsSinItem.getItemRespons());
                    }
                    destroyCommonResult.setItemTotalNumber("1");
                    destroyCommonResult.setArcSign(2);
                    destroyCommonResult.setArcId(volId);
                    destroyCommonResult.setApprCode(apprCode);
                    destroyCommonResult.setApprOldDate(appExtrafive);
                    destroyCommonResult.setApprRemarks(appExtrasix);
                    des.add(destroyCommonResult);
                }
           }
            List<DestroyCommonResult> finalList = new ArrayList<DestroyCommonResult>();
            PageUtils<DestroyCommonResult> pageUtils = new PageUtils<DestroyCommonResult>();
            finalList = pageUtils.pageList(des,offset,limit);
            result.put("rows",finalList);
            result.put("total",des.size());
        return result;
    }

    /**
     * 添加销毁信息
     *
     * @return user
     */
    @ApiOperation(value = "添加销毁信息")
    @RequestMapping(value = "/insertDesMessage")
    @ResponseBody
    public TaotaoResult insertDesMessage(HttpServletRequest request, HttpServletResponse response,String rows) {
        TaotaoResult result = new TaotaoResult();
        String desId = request.getParameter("desId");
        String desCode = request.getParameter("desCode");
        String desApartment = request.getParameter("desApartment");
        String desPerson = request.getParameter("desPerson");
        String desDate = request.getParameter("desDate");
        String desArcview = request.getParameter("desArcview");
        String desPersonview = request.getParameter("desPersonview");
        String desLeaderview = request.getParameter("desLeaderview");
        String desResult = request.getParameter("desResult");
        String signatureOne = request.getParameter("signatureOne");
        String dateOne = request.getParameter("dateOne");
        String signatureTwo = request.getParameter("signatureTwo");
        String dateTwo = request.getParameter("dateTwo");
        String signatureThree = request.getParameter("signatureThree");
        String dateThree = request.getParameter("dateThree");
        List<DestroyCommonResult> destroyCommonResults = JSONArray.parseArray(rows, DestroyCommonResult.class);
        if("".equals(desId)){
            result = arcDestroyService.inserDesMessages(desCode, desApartment, desPerson, desDate, desArcview,
                    desPersonview,desLeaderview,destroyCommonResults,desResult,signatureOne,dateOne,signatureTwo,dateTwo,signatureThree,dateThree);
        }else{
            result = arcDestroyService.editDesMessages(desId,desCode, desApartment, desPerson, desDate, desArcview,
                    desPersonview,desLeaderview,destroyCommonResults,desResult,signatureOne,dateOne,signatureTwo,dateTwo,signatureThree,dateThree);
        }
        return result;
    }
    /**
     * 添加销毁信息
     *
     * @return user
     */
    @ApiOperation(value = "添加销毁信息")
    @RequestMapping(value = "/chooseCodeRepeat")
    @ResponseBody
    public TaotaoResult chooseCodeRepeat(HttpServletRequest request, HttpServletResponse response) {
        TaotaoResult result = new TaotaoResult();
        String desCode = request.getParameter("desCode");
        result = arcDestroyService.chooseCodeRepeat(desCode);
        return result;
    }


    @ApiOperation(value = "查询所有的鉴定信息")
    @RequestMapping(value = "/searchHistoryDesMessage")
    @ResponseBody
    public Object  searchHistoryDesMessage(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        Map<String, Object> result = new HashMap<>(2);
        //查询件
        List<CmsDestroy> cmsDestroy =  arcDestroyService.searchHistoryDesMessage();
        List<CmsDestroy> finalList = new ArrayList<CmsDestroy>();
        PageUtils<CmsDestroy> pageUtils = new PageUtils<CmsDestroy>();
        finalList = pageUtils.pageList(cmsDestroy,offset,limit);
        result.put("rows",finalList);
        result.put("total",cmsDestroy.size());
        return result;
    }

    @ApiOperation(value = "根据案卷号查询销毁信息")
    @RequestMapping(value = "/checkDestroyArcByDesId")
    @ResponseBody
    public Object checkDestroyArcByDesId(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Object>>  list = new ArrayList<>();
        String desId = request.getParameter("desId");
        List<DestroyCommonResult> des = new ArrayList<DestroyCommonResult>();
        List<CmsDestroyArchive> cmsDestroyArc = arcDestroyArchiveService.selectDesArcByDesId(desId);
        for (CmsDestroyArchive cmsDestroyArchive:cmsDestroyArc) {
            String dmExtraone = cmsDestroyArchive.getDmExtraone();
            String dmExtratwo = cmsDestroyArchive.getDmExtratwo();
            String dmExtrathree = cmsDestroyArchive.getDmExtrathree();
            Integer dmArctype = cmsDestroyArchive.getDmArctype();
            Integer dmArcId = cmsDestroyArchive.getDmArcId();
            if(dmArctype == 0){
                DestroyCommonResult  destroyCommonResult = new DestroyCommonResult();
                CmsJudVolume cmsJudVolume = volAndItemService.selectCmsJudVolumeByVolId(Long.valueOf(dmArcId));
                if(!StringUtils.isBlank(cmsJudVolume.getVolClass())){
                    destroyCommonResult.setVolClass(cmsJudVolume.getVolClass());
                }
                if(!StringUtils.isBlank(cmsJudVolume.getVolSecret())){
                    destroyCommonResult.setVolSecret(cmsJudVolume.getVolSecret());
                }
                if(!StringUtils.isBlank(cmsJudVolume.getVolTotalPage())){
                    destroyCommonResult.setVolTotalPage(cmsJudVolume.getVolTotalPage());
                }
                if(!StringUtils.isBlank(cmsJudVolume.getVolStorageTime())){
                    destroyCommonResult.setVolStorageTime(cmsJudVolume.getVolStorageTime());
                }
                if(!StringUtils.isBlank(cmsJudVolume.getVolTitle())){
                    destroyCommonResult.setVolTitle(cmsJudVolume.getVolTitle());
                }
                if(!"".equals(cmsJudVolume.getVolArchiveDate())){
                    destroyCommonResult.setVolArchiveDate(cmsJudVolume.getVolArchiveDate());
                }
                if(!StringUtils.isBlank(cmsJudVolume.getVolCaseNum())){
                    destroyCommonResult.setVolCaseNum(cmsJudVolume.getVolCaseNum());
                }
                destroyCommonResult.setItemName("");
                destroyCommonResult.setItemRespons("");
                Long itemNumById = arcAppraisalService.findItemNumById(dmArcId.toString());
                destroyCommonResult.setItemTotalNumber(itemNumById.toString());
                destroyCommonResult.setArcSign(0);
                destroyCommonResult.setApprCode(dmExtraone);
                destroyCommonResult.setApprOldDate(dmExtratwo);
                destroyCommonResult.setApprRemarks(dmExtrathree);
                destroyCommonResult.setArcId(Long.valueOf(dmArcId));
                des.add(destroyCommonResult);
            }else if(dmArctype == 1){
                DestroyCommonResult  destroyCommonResult = new DestroyCommonResult();
                List<Map<String,Object>>  arichives = arcAppraisalService.findArchiveTypeByArchiveNum(dmArcId.toString());
                for (Map<String,Object> map:arichives) {
                    if(map.containsKey("VOL_CLASS")){
                        destroyCommonResult.setVolClass(map.get("VOL_CLASS").toString());
                    }
                    if(map.containsKey("VOL_SECRET")){
                        destroyCommonResult.setVolSecret(map.get("VOL_SECRET").toString());
                    }
                    //保存期限
                    if(map.containsKey("VOL_STORAGE_TIME")){
                        destroyCommonResult.setVolStorageTime(map.get("VOL_STORAGE_TIME").toString());
                    }
                    if(map.containsKey("ITEM_TITLE")){
                        destroyCommonResult.setVolTitle(map.get("ITEM_TITLE").toString());
                    }
                    if(map.containsKey("ITEM_PAGE")){
                        destroyCommonResult.setVolTotalPage(map.get("ITEM_PAGE").toString());
                    }
                    if(map.containsKey("ITEM_STR11_DATE")){
                        destroyCommonResult.setVolArchiveDate((Date)map.get("ITEM_STR11_DATE"));
                    }
                    if(map.containsKey("ITEM_STR1")){
                        destroyCommonResult.setVolCaseNum(map.get("ITEM_STR1").toString());
                    }
                    if(map.containsKey("ITEM_NAME")){
                        destroyCommonResult.setItemName(map.get("ITEM_NAME").toString());
                    }
                    if(map.containsKey("ITEM_RESPONS")){
                        destroyCommonResult.setItemRespons(map.get("ITEM_RESPONS").toString());
                    }
                    destroyCommonResult.setItemTotalNumber("1");
                    destroyCommonResult.setArcSign(1);
                    destroyCommonResult.setApprCode(dmExtraone);
                    destroyCommonResult.setApprOldDate(dmExtratwo);
                    destroyCommonResult.setApprRemarks(dmExtrathree);
                    destroyCommonResult.setArcId(Long.valueOf(dmArcId));
                    des.add(destroyCommonResult);
                }
            }else{
                DestroyCommonResult  destroyCommonResult = new DestroyCommonResult();
                List<CmsSinItem> cmsSinItems = arcAppraisalService.selectSinItemByItemId(Long.valueOf(dmArcId.toString()));
                CmsSinItem cmsSinItem = cmsSinItems.get(0);
                //案卷分类
                if(!StringUtils.isBlank(cmsSinItem.getItemExp1())){
                    destroyCommonResult.setVolClass(cmsSinItem.getItemExp1());
                }
                //密级
                if(!StringUtils.isBlank(cmsSinItem.getItemSecret())){
                    destroyCommonResult.setVolSecret(cmsSinItem.getItemSecret());
                }
                //页数
                if(!StringUtils.isBlank(cmsSinItem.getItemPage().toString())){
                    destroyCommonResult.setVolTotalPage(cmsSinItem.getItemPage().toString());
                }
                if(!StringUtils.isBlank(cmsSinItem.getItemNum())){
                    destroyCommonResult.setVolStorageTime(cmsSinItem.getItemNum());
                }
                //题名
                if(!StringUtils.isBlank(cmsSinItem.getItemTitle())){
                    destroyCommonResult.setVolTitle(cmsSinItem.getItemTitle());
                }
                //归档日期
                if(!"".equals(cmsSinItem.getItemStr1Date())){
                    destroyCommonResult.setVolArchiveDate(cmsSinItem.getItemStr1Date());
                }
                //档号
                if(!StringUtils.isBlank(cmsSinItem.getItemStr1())){
                    destroyCommonResult.setVolCaseNum(cmsSinItem.getItemStr1());
                }
                //文号
                if(!StringUtils.isBlank(cmsSinItem.getItemName())){
                    destroyCommonResult.setItemName(cmsSinItem.getItemName());
                }
                //责任者
                if(!StringUtils.isBlank(cmsSinItem.getItemRespons())){
                    destroyCommonResult.setItemRespons(cmsSinItem.getItemRespons());
                }
                destroyCommonResult.setItemTotalNumber("1");
                destroyCommonResult.setArcSign(1);
                destroyCommonResult.setApprCode(dmExtraone);
                destroyCommonResult.setApprOldDate(dmExtratwo);
                destroyCommonResult.setApprRemarks(dmExtrathree);
                destroyCommonResult.setArcId(Long.valueOf(dmArcId));
                des.add(destroyCommonResult);

            }
        }
        return des;
    }
    /**
     * 添加销毁信息
     *
     * @return user
     */
    @ApiOperation(value = "添加销毁信息")
    @RequestMapping(value = "/updateResultDesMessage")
    @ResponseBody
    public TaotaoResult updateResultDesMessage(HttpServletRequest request, HttpServletResponse response,String rows) {
        TaotaoResult result = new TaotaoResult();
        String desId = request.getParameter("desId");
        String desMnaview = request.getParameter("desMnaview");
        String desOrgview = request.getParameter("desOrgview");
        String desLeaderview = request.getParameter("desLeaderview");
        String desResult = request.getParameter("desResult");
        String signatureOne = request.getParameter("signatureOne");
        String dateOne = request.getParameter("dateOne");
        String signatureTwo = request.getParameter("signatureTwo");
        String dateTwo = request.getParameter("dateTwo");
        String signatureThree = request.getParameter("signatureThree");
        String dateThree = request.getParameter("dateThree");
        result = arcDestroyService.updateDesMessages(desId, desMnaview, desOrgview, desLeaderview, desResult,signatureOne, dateOne, signatureTwo,dateTwo,signatureThree,dateThree);
        return result;
    }
}